const Router = require('koa-router')
const { error, INFO } = require('./utils/error')
const { settings, ipRegex } = require('./utils')
const {
  requestLogin,
  requestUser,
  requestUserClose,
  requestAuditNewsList,
  requestNews,
  requestAudit,
  requestNewsComplaint,
  requestNewsList,
  requestNewsState,
  requestPostBanner,
  requestPutBanner,
  requestGetBanner,
  requestClassConfig,
  requestDeleteClass,
  requestPostNewsComplaint,
  requestAddClass
} = require('./api/admin')
const apiError = (info) => {
  error('50000', __filename, info)
}
const router = new Router({
  prefix: '/api/admin'
})
const token = 'information-youth-admin'
router.post('/login', async (ctx) => {
  const { username, password } = ctx.request.body
  const { status, data } = await requestLogin({
    username,
    pwd: password
  })
  let code = -1
  let msg = ''
  let name = ''
  let id = ''
  try {
    if (status === 200 && data.statu === 0) {
      code = 0
      name = data.data.name
      id = data.data.id
    } else {
      code = -1
    }
  } catch (error) {
    apiError('POST-login')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg,
      data: {
        token,
        name,
        id
      }
    }
  }
})
router.post('/logout', (ctx) => {
  ctx.body = {
    code: 0,
    msg: 'success'
  }
})
router.get('/user', async (ctx) => {
  const { email, name, id, page, limit } = ctx.query
  const { status, data } = await requestUser({
    email,
    name,
    id,
    curr: page,
    limit
  })
  let code = -1
  let msg = ''
  let len = 100
  let result = []
  try {
    if (status === 200 && data.statu === 0) {
      code = 1
      result = data.data.map((item) => {
        return {
          id: item.id,
          name: item.name,
          email: item.email,
          time: item.date,
          ban: item.isBan === 1
        }
      })
      len = +data.msg
    } else {
      code = -1
    }
  } catch (error) {
    apiError('GET-user')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg,
      len,
      data: result
    }
  }
})
router.post('/userClose', async (ctx) => {
  const { id, op } = ctx.request.body
  const { status, data } = await requestUserClose({
    id,
    op
  })
  let code = -1
  let msg = ''
  try {
    if (status === 200 && data.statu === 0) {
      code = 1
    } else {
      code = -1
      msg = data.msg
    }
  } catch {
    apiError('POST-userClose')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg
    }
  }
})
router.get('/news/audit', async (ctx) => {
  const { page, limit } = ctx.query
  const { status, data } = await requestAuditNewsList({
    curr: page,
    limit
  })
  let code = -1
  let msg = ''
  let len = 100
  let result = []
  try {
    if (status === 200 && data.statu === 0) {
      code = 1
      result = data.data.map((item) => {
        return {
          id: item.id,
          title: item.title,
          time: item.date
        }
      })
      len = +data.msg
    } else {
      code = -1
    }
  } catch (error) {
    apiError('GET-news/audit')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg,
      len,
      data: result
    }
  }
})

router.get('/news/list', async (ctx) => {
  const { id, title, page, limit, state, name, deleted } = ctx.query
  const plateId = ctx.query['class[]'] ? ctx.query['class[]'][0] : ''
  const classId = ctx.query['class[]'] ? ctx.query['class[]'][1] : ''
  let code = -1
  let msg = ''
  let result = []
  let len = 100
  try {
    const { status, data } = await requestNewsList({
      newsId: id,
      title,
      plateId,
      classId,
      isDelete: deleted,
      tag: state,
      username: name,
      curr: page,
      limit
    })
    if (status === 200 && data.statu === 0) {
      code = 1
      result = data.data.map((item) => {
        return {
          id: item.id,
          title: item.title,
          classId: item.plateId,
          mapId: item.classId,
          user: {
            id: item.userId,
            name: item.userName
          },
          state: {
            name: item.tag,
            delete: item.isDelete,
            banner: item.isTop
          },
          browse: item.browse,
          comments: item.comments,
          time: item.date
        }
      })
      len = +data.msg
    } else {
      code = -1
      msg = data.msg
    }
  } catch (error) {
    apiError('GET-news/list')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg,
      len,
      data: result
    }
  }
})
router.get('/news/complaint', async (ctx) => {
  const { page, limit } = ctx.query
  const { status, data } = await requestNewsComplaint({
    curr: page,
    limit
  })
  let code = -1
  let msg = ''
  let result = []
  let len = 100
  try {
    if (status === 200 && data.statu === 0) {
      code = 1
      result = data.data.map((item) => {
        return {
          id: item.id,
          user: {
            id: item.userId,
            name: item.userName
          },
          news: {
            id: item.articleId
          },
          content: item.content
        }
      })
      len = +data.msg
    } else {
      code = -1
      msg = data.msg
    }
  } catch (error) {
    apiError('GET-news/list')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg,
      len,
      data: result
    }
  }
})
router.post('/news/complaint', async (ctx) => {
  const { id, admin, user, content } = ctx.request.body
  const { status, data } = await requestPostNewsComplaint({
    id,
    admId: admin,
    userId: user,
    content
  })
  let code = -1
  let msg = ''
  try {
    if (status === 200 && data.statu === 0) {
      code = 0
    } else {
      code = -1
    }
  } catch (error) {
    apiError('POST-news/audit')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg
    }
  }
})
router.get('/news', async (ctx) => {
  const { id } = ctx.query
  const { status, data } = await requestNews({
    id
  })
  let code = -1
  let msg = ''
  let result
  try {
    if (status === 200 && data.statu === 0) {
      code = 1
      const item = data.data
      result = {
        title: item.title,
        content: item.content.replace(ipRegex, settings.URL__STATIC)
      }
    } else {
      code = -1
    }
  } catch (error) {
    apiError('GET-news')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg,
      data: result
    }
  }
})
router.post('/audit', async (ctx) => {
  const { id, audit, reason } = ctx.request.body
  const { status, data } = await requestAudit({
    id,
    op: audit ? -1 : 1,
    content: reason
  })
  let code = -1
  let msg = ''
  try {
    if (status === 200 && data.statu === 0) {
      code = 0
    } else {
      code = -1
      msg = data.msg
    }
  } catch (error) {
    apiError('POST-audit')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg
    }
  }
})
router.post('/banner', async (ctx) => {
  const { id } = ctx.request.body
  let code = -1
  let msg = ''
  try {
    const { status, data } = await requestPostBanner({
      id
    })
    if (status === 200) {
      if (data.statu === 0) {
        code = 0
      } else {
        code = -1
        msg = data.msg
      }
    }
  } catch (error) {
    apiError('POST-banner/set')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg
    }
  }
})
router.get('/banner', async (ctx) => {
  const { id } = ctx.query
  let code = -1
  let msg = ''
  let result = []
  try {
    const { status, data } = await requestGetBanner({
      id
    })
    if (status === 200) {
      if (data.statu === 0) {
        code = 1
        result = data.data.map((item) => {
          return {
            id: item.id,
            title: item.title,
            imgUrl: settings.URL__UPLOAD__NEWS + '/' + item.img
          }
        })
      } else {
        code = -1
        msg = data.msg
      }
    }
  } catch (error) {
    apiError('GET-banner')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      data: result,
      msg
    }
  }
})
router.put('/banner', async (ctx) => {
  let code = -1
  let msg = ''
  try {
    const { status, data } = await requestPutBanner({
      data: ctx.request.body.data
    })
    if (status === 200) {
      if (data.statu === 0) {
        code = 0
      } else {
        code = -1
        msg = data.msg
      }
    }
  } catch (error) {
    apiError('PUT-banner')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg
    }
  }
})
router.post('/news/state', async (ctx) => {
  const { id, op } = ctx.request.body
  let code = -1
  let msg = ''
  try {
    const { status, data } = await requestNewsState({
      id,
      op
    })
    if (status === 200 && data.statu === 0) {
      code = 0
    } else {
      code = -1
      msg = data.msg
    }
  } catch (error) {
    apiError('POST-news/state')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg
    }
  }
})
router.get('/public/class', async (ctx) => {
  let code = -1
  let msg = ''
  let result = []
  try {
    const { status, data } = await requestClassConfig()
    if (status === 200 && data.statu === 0) {
      code = 1
      result = data.data.map((item) => {
        return {
          id: item.id,
          name: item.name,
          delete: item.isDelete === 1,
          map: item.class.map((item) => {
            return {
              id: item.id,
              name: item.name,
              delete: item.isDelete === 1
            }
          })
        }
      })
    } else {
      code = -1
      msg = data.msg
    }
  } catch (error) {
    apiError('POST-public/class')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      data: result,
      msg
    }
  }
})
router.delete('/public/class', async (ctx) => {
  const { id, op } = ctx.request.body
  let code = -1
  let msg = ''
  try {
    const { status, data } = await requestDeleteClass({
      id,
      op
    })
    if (status === 200 && data.statu === 0) {
      code = 0
    } else {
      code = -1
      msg = data.msg
    }
  } catch (error) {
    apiError('DELETE-public/class')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg
    }
  }
})
router.post('/public/class', async (ctx) => {
  const { op, name } = ctx.request.body
  let code = -1
  let msg = ''
  try {
    const { status, data } = await requestAddClass({
      op,
      name
    })
    if (status === 200 && data.statu === 0) {
      code = 0
    } else {
      code = -1
      msg = data.msg
    }
  } catch (error) {
    apiError('POST-public/class')
    msg = INFO['50000']
  } finally {
    ctx.body = {
      code,
      msg
    }
  }
})
module.exports = router
